//
//  WmcYupaoNetwork.swift
//  YPWatermarkCamera
//
//  Created by 马涛 on 2022/6/24.
//

import Alamofire
import KeychainAccess
import Moya
import UIKit
import YPBedrock
import YPNetwork

/**
 多媒体四套环境域名地址：
 开发：https://yupao-develop.vrtbbs.com/media
 测试：https://yupao-test.yupaowang.com/media
 预发布：https://yupao-master.kkbbi.com/media
 生产：https://yupao-prod.54xiaoshuo.com/media
 */

class WmcYupaoRequest: YPNetworkRequest {
    // Internal

    override var host: String {
        var text = "https://wmc.cdzbb.cn"
        let environmentType = YPWmcManager.shared.environmentType
        switch environmentType {
        case .develop:
            text = "https://yupao-develop.vrtbbs.com"
        case .test:
            text = "https://yupao-test.yupaowang.com"
        case .pre_release:
            text = "https://yupao-master.kkbbi.com"
        case .release:
            text = "https://yupao-prod.54xiaoshuo.com"
        }
        return text
    }

    override var clientInfo: [String: String] {
        var channel = "AppStore"
        #if DEBUG
            channel = "AppStore"
        #endif
        var header: [String: String] = [
            "appId": YPWmcManager.shared.appId, // 业务id
            "source": YPWmcManager.shared.platform.info.sourceField, // 来源
            "channel": channel, // 渠道包
            "imei": Self.uuid, // imei设备标识
            "platform": "ios", // 平台
            "system": "IOS", // 系统类型
            "appVersion": YPApp.Version, // app应用版本
            "wmcVersion": YPWmcManager.shared.version, // 水印相机应用版本
            "sdkVersion": YPWmcManager.shared.mediaSdkVersion, // 多媒体SDK版本
            "systemVersion": UIDevice.current.systemVersion, // 操作系统版本
            "system_type": "ios",
            "business": "5",
            "version": YPWmcManager.shared.version
        ]
        
        header.merge(YPWmcManager.shared.yupaoEextensionHeaders, uniquingKeysWith: {$1})
        return header
    }

    override var userInfo: [String: String] {
        var result: [String: String] = [:]
        if let yupaoAccountModel = WmcLoginManager.shared.yupaoAccountModel {
            result["token"] = yupaoAccountModel.token // 用户登录态token
            result["singletoken"] = yupaoAccountModel.singleSignToken
        }

        return result
    }

    override var responseField: [YPNetworkRespnseField: String] {
        return [
            .messageField: "message",
            .codeField: "code",
            .modelField: "data",
            .listField: "list",
        ]
    }

    override var successInterceptor: YPNetworkInterceptor {
        return YPNetworkInterceptor.create(type: .success, code: [0])
    }

    override var loginFailureInterceptor: YPNetworkInterceptor? {
        return YPNetworkInterceptor.create(type: .loginFailure, code: [401]) { (data: Any?, code: Int64, message: String?) in
            WmcLoginManager.logout()

            if YPWmcManager.shared.isIndependentPage {
                WmcLoginManager.push(animated: true)
                return
            }

            YPWmcManager.shared.delegate.loginDelegate?.loginInvalid(.current, data: data, code: code, message: message)
        }
    }
    // Private

    private static var uuid: String = {
        let key = "uuid"
        guard let info = Bundle.main.infoDictionary else { return "" }
        guard let identifier = info["CFBundleIdentifier"] as? String else { return "" }

        let keychain = Keychain(service: identifier)
        guard let uuid = keychain[key] else {
            let uuid: String? = UIDevice.current.identifierForVendor?.uuidString
            keychain[key] = uuid
            return uuid ?? "无UUID"
        }
        return uuid
    }()
}
